#include "stdafx.h"
#include "RayTraceMgr.h"

static int year = 0, day = 0;
int g_nWidth, g_nHeight;

void init(void) 
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_FLAT);
}

void display(void)
{
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 1.0, 1.0);

//     glBegin(GL_TRIANGLE_STRIP);
//     glVertex3f(0, 0, 0);
//     glVertex3f(10, 0, 0);
//     glVertex3f(5, 10, 0);
//     glEnd();

    RayTraceMgr::GetInstance()->Render();

    //glLoadIdentity();
    //glTranslated(-16, -16, -30);
    //glutSolidSphere(10, 50, 50);

    //glutSwapBuffers();
}

void reshape (int w, int h)
{
	// If want to set width and height, RayTraceMgr has to reallocate color buffer.
    glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D(0, w, 0, h);
}

void keyboard (unsigned char key, int x, int y)
{
    switch (key) {
      case 'd':
          day = (day + 10) % 360;
          glutPostRedisplay();
          break;
      case 'D':
          day = (day - 10) % 360;
          glutPostRedisplay();
          break;
      case 'y':
          year = (year + 5) % 360;
          glutPostRedisplay();
          break;
      case 'Y':
          year = (year - 5) % 360;
          glutPostRedisplay();
          break;
      case 27:
          exit(0);
          break;
      default:
          break;
    }
}

int test(){
	float eta = 1;
	Vector3f N(0, 0, 1);
	Vector3f I = /*Normalize*/(Vector3f(1, -1, -1));
	float k = 1.0 - eta * eta * (1.0 - Dot(N, I) * Dot(N, I));
	Vector3f R;
	if (k < 0.0)
		R = Vector3f();       // or genDType(0.0)
	else
		R = I * eta - N * (eta * Dot(N, I) + sqrt(k));
	R;
	return 0;
}
int main(int argc, char** argv)
{
	//return test();
	
    g_nWidth = 600;
    g_nHeight = 600;
    RayTraceMgr::GetInstance()->Init(g_nWidth, g_nHeight, Vector3f(0, 0, 30), 60);
    RayTraceMgr::GetInstance()->Generate();

    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize (g_nWidth, g_nHeight); 
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display); 
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutMainLoop();

	return 0;
}
